package com.ruoyi.web.controller.aggregation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.aggregation.ISmsService;

/**
 * 短信验证码Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/sms")
public class SmsController
{
    @Autowired
    private ISmsService smsService;

    /**
     * 发送短信验证码
     */
    @Anonymous
    @GetMapping("/sendCode")
    public AjaxResult sendCode(@RequestParam String phone)
    {
        return send(phone);
    }

    /**
     * 发送短信验证码（别名接口，兼容前端调用）
     */
    @Anonymous
    @GetMapping("/send")
    public AjaxResult send(@RequestParam String phone)
    {
        try
        {
            if (phone == null || phone.trim().isEmpty())
            {
                return AjaxResult.error("手机号不能为空");
            }

            // 简单的手机号验证
            if (!phone.matches("^1[3-9]\\d{9}$"))
            {
                return AjaxResult.error("手机号格式不正确");
            }

            boolean success = smsService.sendCode(phone);
            if (success)
            {
                return AjaxResult.success("验证码已发送");
            }
            else
            {
                return AjaxResult.error("验证码发送失败");
            }
        }
        catch (Exception e)
        {
            return AjaxResult.error("发送失败：" + e.getMessage());
        }
    }
}

